<?php
// +----------------------------------------------------------------------
// | ZanPianCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.zanpian.com All rights reserved.
// +----------------------------------------------------------------------
// | BBS:  <http://www.feifeicms.cc>
// +----------------------------------------------------------------------
//赞片CMS
namespace app\common\model;
use think\Model;
use think\Cache;
class Actor extends Model{
    // 定义时间戳字段名
    protected $createTime = '';
    protected $updateTime = '';
	// 自动完成
	protected $auto = [];
	protected $insert = ['actor_addtime'];
	protected $update = [];	
	//自动完成相关字段
	protected function setActorAddtimeAttr($value,$data){
		if (!empty($data['actor_checktime']) || empty($value)) {
			return time();
		}else{
			return strtotime($value);
		}
	}
	protected function setActorCidAttr($value,$data){
		if ($value) {
			return $value;
		}else{
			return model('channel')->get_type_cid(6);
		}
	}
    protected function setActorPrtyAttr($value){
        if(!is_array($value)){
            return 0;
        }else{
            $attribute = 0;
            foreach ($value as $key=>$value){
                $attribute += $value;
            }
            return $attribute;
        }
    }		
	protected function setActorkeywordsAttr($value,$data){
		if(!empty($value)){
			return $value;
		}elseif(empty($value) && !empty($data['actor_vid']) && !empty($data['auto_keywords'])){
			$get=controller('get');
			$info=$get->keywords(db('vod')->where('vod_id',$data['actor_vid'])->value('vod_name').'演员',1);
			if($info['code']=1){
			    return $info['data']['str'];	
			}else{
				return '';
			}
		}else{
			return '';
		}
	}
	//事件注册
    protected static function init(){
		//写入后置事件
		Actor::afterWrite(function ($actor){
			if(!empty($actor->actor_id)){
			   model('Manage')->DelCache(6,$actor->actor_id);
			}
		});			
		//删除后置操作
        Actor::afterDelete(function($actor){
			//删除关联角色
			if(!empty($actor->actor_vid)){	
			    $roleid=db('role')->where('role_vid',$actor->actor_vid)->column('role_id');
		        model('role')->destroy($roleid);
			}
			 model('Manage')->DelCache(6,$actor->actor_id);
       });	
    }	
	public function vod(){
		return $this->hasOne('vod','vod_id','actor_vid');
    }
    public function role(){
        return $this->hasMany('role','role_vid','actor_vid');
    }
	public function role_count($vid){
		if(!empty($vid)){
		    return db('role')->where('role_vid',$vid)->count('role_id');
		}else{
			return 0;
		}
    }
    public function listData($tag){
		$param = zanpiancms_param_url();
		if(!is_array($tag)) {
            $tag=json_decode($tag, true);
        }
		$tag['field']= !empty($tag['field']) ? $tag['field'] : 'actor.*,vod_id,vod_cid,vod_mcid,vod_name,vod_aliases,vod_title,vod_color,vod_actor,vod_director,vod_content,vod_pic,vod_bigpic,vod_diantai,vod_tvcont,vod_area,vod_language,vod_year,vod_continu,vod_total,vod_isend,vod_addtime,vod_hits,vod_hits_day,vod_hits_week,vod_hits_month,vod_hits_lasttime,vod_stars,vod_up,vod_down,vod_play,vod_gold,vod_golder,vod_isfilm,vod_filmtime,vod_length,vod_weekday,vod_letters,vod_bigpic,vod_label,vod_jumpurl';
		$tag['limit']= !empty($tag['limit']) ? $tag['limit'] : 10;
		if(!empty($tag['page']) && !empty($param['order'])){
			$tag['order']='actor_'.$param['order'].' desc';
		}			
		if(empty(order_check($tag['order']))){
			$tag['order']='actor_addtime desc';
		}		
		$cahce=tag_cache($tag);
	    //优先从缓存调用数据及分页变量
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){			
			$data_cache_content = Cache::get($cahce['cache_name']);
			if($data_cache_content){
				return $data_cache_content;
			}
		}		
		$where=[];
		$where['actor_status']=['eq',1];
		$where['vod_status']=['eq',1];
	    if(!empty($tag['ids'])){
		     $where['actor_id'] = array('in',$tag['ids']);
	    }
	    if(!empty($tag['vid'])){
		     $where['actor_vid'] = array('in',$tag['vid']);
	    }		
	    if(!empty($tag['not'])){
		     $where['actor_id'] = array('not in',$tag['not']);
	    }
	    if(!empty($tag['vnot'])){
		     $where['actor_vid'] = array('not in',$tag['vnot']);
	    }		
	    if (!empty($tag['letter'])) {
		    $letter = explode(',',$tag['letter']);
		    if(count($letter)>1) {
			    $where['vod_letter'] = array('in',$tag['letter']);
		    }else{
			    $where['vod_letter'] = array('eq',$letter[0]);
		    }
	    }		
	    if(!empty($tag['cid'])) {
		    $cids = explode(',',trim($tag['cid']));
		    if(count($cids) > 1) {
			    $where['actor_cid'] = array('in',getlistarr_tag($cids));
		    }else{
			     $where['actor_cid'] = getlistsqlin($tag['cid']);
		    }
	    }
	    if(!empty($tag['vcid'])) {
		    $cids = explode(',',trim($tag['vcid']));
		    if(count($cids) > 1) {
			    $where['vod_cid'] = array('in',getlistarr_tag($cids));
		    }else{
			     $where['vod_cid'] = getlistsqlin($tag['vcid']);
		    }
	    }		
	    if(!empty($tag['stars'])) {
		     $where['actor_stars'] = array('in',$tag['stars']);
	    }
	    if(!empty($tag['vstars'])) {
		     $where['vod_stars'] = array('in',$tag['vstars']);
	    }		
	    if (!empty($tag['hits'])) {
		    $hits = explode(',',$tag['hits']);
		    if (count($hits) > 1) {
			    $where['actor_hits'] = array('between',$hits[0].','.$hits[1]);
		    }else{
			    $where['actor_hits'] = array('gt',$hits[0]);
		    }
	    }
	    if (!empty($tag['vhits'])) {
		    $hits = explode(',',$tag['vhits']);
		    if (count($hits) > 1) {
			    $where['vod_hits'] = array('between',$hits[0].','.$hits[1]);
		    }else{
			    $where['vod_hits'] = array('gt',$hits[0]);
		    }
	    }		
	    if(!empty($tag['name'])) {
		    $where['vod_name'] = array('like','%'.$tag['name'].'%');	
	    }
	    if(!empty($tag['wd'])) {
		    $where['vod_name'] = array('like','%'.$tag['wd'].'%');	
	    }
        if(!empty($tag['gold'])) {
		    $gold = explode(',',$tag['gold']);
		    if(count($gold) > 1) {
			    $where['actor_gold'] = array('between',$gold[0].','.$gold[1]);
		    }else{
			    $where['actor_gold'] = array('gt',$gold[0]);
		    }
	    }
        if(!empty($tag['vgold'])) {
		    $gold = explode(',',$tag['vgold']);
		    if(count($gold) > 1) {
			    $where['vod_gold'] = array('between',$gold[0].','.$gold[1]);
		    }else{
			    $where['vod_gold'] = array('gt',$gold[0]);
		    }
	    }		
	    if(!empty($tag['golder'])) {
		    $golder = explode(',',$tag['golder']);
		    if(count($golder) > 1) {
			    $where['actor_golder'] = array('between',$golder[0].','.$golder[1]);
		    }else{
			    $where['actor_golder'] = array('gt',$golder[0]);
		    }
	    }
	    if(!empty($tag['vgolder'])) {
		    $golder = explode(',',$tag['vgolder']);
		    if(count($golder) > 1) {
			    $where['vod_golder'] = array('between',$golder[0].','.$golder[1]);
		    }else{
			    $where['vod_golder'] = array('gt',$golder[0]);
		    }
	    }		
	    if(!empty($tag['up'])) {
		    $up = explode(',',$tag['up']);
		    if(count($up)>1) {
			    $where['actor_up'] = array('between',$up[0].','.$up[1]);
		    }else{
			    $where['actor_up'] = array('gt',$up[0]);
		    }
	    }
	    if(!empty($tag['vup'])) {
		    $up = explode(',',$tag['vup']);
		    if(count($up)>1) {
			    $where['vod_up'] = array('between',$up[0].','.$up[1]);
		    }else{
			    $where['vod_up'] = array('gt',$up[0]);
		    }
	    }		
	    if(!empty($tag['down'])) {
		    $down = explode(',',$tag['down']);
		    if(count($down)>1) {
			    $where['actor_down'] = array('between',$down[0].','.$down[1]);
		    }else{
			    $where['actor_down'] = array('gt',$down[0]);
		    }
	    }
	    if(!empty($tag['vdown'])) {
		    $down = explode(',',$tag['vdown']);
		    if(count($down)>1) {
			    $where['vod_down'] = array('between',$down[0].','.$down[1]);
		    }else{
			    $where['vod_down'] = array('gt',$down[0]);
		    }
	    }		
        if(!empty($tag['addtime'])){
		    $time = explode(',',$tag['addtime']);
		    if(count($time) > 1) {
			    $where['actor_addtime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['actor_addtime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }
        if(!empty($tag['vaddtime'])){
		    $time = explode(',',$tag['vaddtime']);
		    if(count($time) > 1) {
			    $where['vod_addtime'] = array('between',intval(strtotime($time[0])).','.intval(strtotime($time[1])));
		    }else{
			    $where['vod_addtime'] = array('gt',intval(strtotime($time[0])));
		    }			
        }
	    if(!empty($tag['filmtime'])){
			if($tag['filmtime']==1){
		        //$where['vod_filmtime'] = array('between',array(strtotime("-6 month",time()),time()));
				$where['vod_filmtime'] = array('elt',time());
			}else{
				$tjinfo=explode('|',trim($tag['filmtime']));
			    if($tjinfo[1]){
				    $where['vod_filmtime'] = array('between',array(intval($tjinfo[0]),intval($tjinfo[1])));
			    }else{
		            $where['vod_filmtime'] = array('gt', time());
			    }
			}
	    }
	    if(!empty($tag['tj'])){
		    $tjinfo=explode('|',trim($tag['tj']));
		    if($tjinfo[3]){
			    $where[$tjinfo[3]] = array($tjinfo[1],$tjinfo[2]);
		    }else{
			    $where[$tjinfo[0]] = array($tjinfo[1],$tjinfo[2]);
		    }
	    }		
		if($tag['prty']){
			if(!empty(check_position($tag['prty']))){
			    $where['actor_prty'] = check_position($tag['prty']);
			}
		}
		if($tag['vprty']){
			if(!empty(check_position($tag['vprty']))){
			    $where['vod_prty'] = check_position($tag['vprty']);
			}
		}		
        if(!empty($tag['page'])){
		    $data = db('actor')->alias('actor')->join('vod vod','vod.vod_id =actor_vid','LEFT')->where($where)->field($tag['field'])->order($tag['order'])->paginate($tag['limit'],false);
		}else{
			$data = db('actor')->alias('actor')->join('vod vod','vod.vod_id =actor_vid','LEFT')->where($where)->field($tag['field'])->order($tag['order'])->limit($tag['limit'])->select();
		}
		if(!empty($cahce['cache_name']) && !empty($cahce['cache_time'])){
			cache::tag('foreach_actor')->set($cahce['cache_name'],$data,intval($cahce['cache_time'])); 
	    }
		return $data;
    } 
	
}